/*******************************************************************************
*  file    : transform_change_freq.cpp
*  created : 27.06.2014
*  author  : Slyshyk Oleksiy (alexSlyshyk@gmail.com)
*******************************************************************************/

#include "transform_change_freq.hpp"
#include <QDebug>
#include <algorithm>
#include <QVariantMap>

#include <utils.hpp>


TransformChangeFreq::TransformChangeFreq(QObject *parent) :
    QObject(parent)
{
}

QString TransformChangeFreq::name() const
{
    return QStringLiteral("Change Freq");
}

NamedData *TransformChangeFreq::transform(const NamedData *data, const QVariant &params)
{
    QScopedPointer<plugin_utils::TransformParamsPrivate> data_;
    data_.reset(new plugin_utils::TransformParamsPrivate );
    data_->setParams(params);

    NamedData* res = new NamedData;
    res->name = data->name + "_ch_freq";
    res->pantry["id"] = params.toMap()["id"];

    transform_1(data->data, data, res, data_.data());

    return res;

    Q_UNUSED(params);
}

QStringList TransformChangeFreq::patternList() const
{
    return plugin_utils::fileList(QStringLiteral("pattern*.dat"));
}

void TransformChangeFreq::transform_1(const std::vector<std::complex<double> > &sample, const NamedData *data, NamedData *res, plugin_utils::TransformParamsPrivate *data_)
{
    if(data_->freqCoef() == 0)
        data_->setSampleRate(1.0);

    res->sampleRate = data->sampleRate * data_->freqCoef();
    res->data.resize( data->data.size() * data_->freqCoef());

    double start_time = 0.0;
    double dt = 1.0 / data_->freqCoef();
    double dt_data = 1.0 ;

    const std::vector<std::complex<double> > &dat = data->data;
    double point_time;
    int32_t iter_count = 0;

    if(data_->smooth().contains("linear"))
        for(uint idx_data = 0; idx_data < res->data.size() ; ++idx_data)
            {
                point_time = (idx_data)*(1.0 / res->sampleRate);
                if(point_time < data_->zoneBegin() || point_time > data_->zoneEnd())
                    res->data[idx_data] = 0.0;
                else
                    {
                        res->data[idx_data] = plugin_utils::get_func_value_linear(dat, start_time, dt_data, idx_data * dt );
                        iter_count++ ;
                    }

            }
    else if(data_->smooth().contains("square"))
        for(uint idx_data = 0; idx_data < res->data.size() ; ++idx_data)
            {
                point_time = (idx_data)*(1.0 / res->sampleRate);
                if(point_time < data_->zoneBegin() || point_time > data_->zoneEnd())
                    res->data[idx_data] = 0.0;
                else
                    {
                        res->data[idx_data] = plugin_utils::get_func_value_square(dat, start_time, dt_data, idx_data * dt);
                        iter_count++ ;
                    }

            }


    res->pantry["iter_count"] = iter_count ;
    qDebug() << "return data zise = " << res->data.size();
    Q_UNUSED(sample)
}
